package pl.veneo.wigprofit.model {
	import pl.veneo.wigprofit.ctrl.events.ModelUpdateEvent;
	import pl.veneo.wigprofit.model.items.AbstractDataItem;
	import pl.veneo.wigprofit.model.items.IndexData;
	import pl.veneo.wigprofit.model.items.PercentData;
	import pl.veneo.wigprofit.tools.TimeSpan;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartDataModel extends EventDispatcher {
		private var _indexes : Array;
		private var _percents : Array;
		private var _url : String;

		public function ChartDataModel(url : String) {
			super();
			_url = url;
		}

		public function updateData() : void {
			_percents = new Array();
			_indexes = new Array();
			var xmlparser : XMLDataParser = new XMLDataParser(this);
			xmlparser.addEventListener(XMLDataParser.ON_DATA_PARSED, sortArrays);
			xmlparser.loadData(_url);
		}

		public function pushIndex(item : IndexData) : void {
			_indexes.push(item);
		}

		public function pushPercent(item : PercentData) : void {
			_percents.push(item);
		}

		private function sortArrays(event : Event) : void {
			var xmlparser : XMLDataParser = event.target as XMLDataParser;
			xmlparser.removeEventListener(XMLDataParser.ON_DATA_PARSED, sortArrays);
			_indexes.sortOn('timestamp', Array.NUMERIC);
			_percents.sortOn('timestamp', Array.NUMERIC);

			var firstDay : Date = (_indexes[0] as IndexData).date;
			var lastDay : Date = (_indexes[_indexes.length - 1] as IndexData).date;

			var ts : TimeSpan = TimeSpan.fromDates(firstDay, lastDay);
			trace("Ilość dni: " + ts.days + " total:" + ts.totalDays);

			dispatchEvent(new ModelUpdateEvent(ModelUpdateEvent.ON_DATA_UPDATED, firstDay, lastDay));
		}

		public function get url() : String {
			return _url;
		}

		public function getDataForPeriod(start : Date, end : Date) : PeriodData {
			var period : PeriodData = new PeriodData();
			period.startDay = start;
			period.endDay = end;
			period.days = TimeSpan.fromDates(start, end).days;

			var _indexesPeriod : Array = new Array();
			var _percentPeriod : Array = new Array();

			var i : int;
			var item : AbstractDataItem;
			var minIndex : Number = 10000000000000000000000;
			var maxIndex : Number = 0;

			var minPercent : Number = 100;
			var maxPercent : Number = 0;

			for (i = 0; i < _indexes.length;i++) {
				item = _indexes[i] as AbstractDataItem;

				if (item.date >= start && item.date <= end) {
					_indexesPeriod.push(item as IndexData);
					_percentPeriod.push(_percents[i]);

					minIndex = (item as IndexData).index < minIndex ? (item as IndexData).index : minIndex;
					maxIndex = (item as IndexData).index > maxIndex ? (item as IndexData).index : maxIndex;

					minPercent = (_percents[i] as PercentData).percent < minPercent ? (_percents[i] as PercentData).percent : minPercent;
					maxPercent = (_percents[i] as PercentData).percent > maxPercent ? (_percents[i] as PercentData).percent : maxPercent;
				}
			}

			period.indexes = _indexesPeriod;
			period.percents = _percentPeriod;
			period.minIndex = minIndex;
			period.maxIndex = maxIndex;
			period.minPercent = minPercent;
			period.maxPercent = maxPercent;

			return period;
		}
	}
}
